//市场部周计划
Ext.namespace("AuditPlan");

AuditPlan.WeeklyReport = function(p) {	
	this.winParent = p;
	this.loadMask = null;	
	this.planIds=null;
	thiz=this;
	this.planName = new Ext.form.TextField({
		fieldLabel: '计划名称',
		name:'planName',
		allowBlank: false,
		width: 580
	});
	this.planType = new Ext.form.Hidden({name:'planType'});
	this.auditorStore=new Ext.data.JsonStore({
	    autoDestroy: true,
	    root:'auditors',
	    url: '../myplan/selectAuditors.form?timesnap=' + Date.parse(new Date()),
	    fields: ['userId', 'userName' ]
	});
	this.auditorStore.load();
	this.auditorId = new Ext.form.ComboBox({
		width: 120,
		fieldLabel: '审核人',
		name:'auditorId',
		store:this.auditorStore,
        valueField: 'userId',
        displayField: 'userName',
        allowBlank: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	this.startDate = new Ext.form.DateField({
    	fieldLabel: '计划时间',
    	width: 120,
    	name:'startDate',
    	allowBlank: false,
		format: 'Y-m-d'
	});
    this.endDate = new Ext.form.DateField({
    	fieldLabel: '～',
    	width: 120,
    	name:'endDate',
    	allowBlank: false,
    	labelWidth: 20,
    	labelAlign: 'center',
    	labelSeparator: '',
		format: 'Y-m-d'
	});
	
	this.form = new Ext.FormPanel({
        region: 'north',
        bodyStyle: 'padding: 5px 5px 0',
        height: 60,
        labelWidth: 70,
        labelAlign: 'right',
        items: [this.planType,this.planName,
	        { layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .36, items: [this.auditorId] },
	        			{ columnWidth: .30, items: [this.startDate]},
	        	            { columnWidth: .30, items: [this.endDate] }]
	    }]
    });
	
	this.renderSource = function(value){
		return OMS.TASKORIGIN[value];
	};
	this.renderCategory = function(value){
		return OMS.PLANCATEGORY[value];
	};
	this.renderStatus = function(value){
		return OMS.TASKSEXESTATUS[value];
	};
	this.renderContent=function(value){
		return Ext.util.Format.ellipsis(value,20,'...');
	}
	this.storeItems = new Ext.data.JsonStore({
	    url: Utils.formatURL('../auditplan/listSummaryItems.form?&timesnap=' + Date.parse(new Date())),
	    root: 'result',
	    fields: ['itemId','planId','taskId','taskStatus','managerId','managerName','itemCategory','itemTitle','itemOther','itemNation','itemRemark','workSource','itemContent','planWork','planFinish']
	});
	
	this.sm =  new Ext.grid.CheckboxSelectionModel();
	this.columns = [
		  this.sm,
		  {header: '明细编号',  align: 'center', width: 20,hidden:true, dataIndex: 'itemId'},
		  {header: '计划编号',  align: 'center', width: 20,hidden:true, dataIndex: 'planId'},
		  {header: '任务编号',  align: 'center', width: 20,hidden:true, dataIndex: 'taskId'},
		  {header: '责任人ID',  align: 'center', width: 40,hidden:true, dataIndex: 'managerId'},
		  {header: '责任人',  align: 'center', width: 30, dataIndex: 'managerName'}, 	      
 	      {header: '工作来源',  align: 'center', width: 25, dataIndex: 'workSource',renderer:this.renderSource}, 
 	      {header: '名称',  align: 'left', width: 40,dataIndex: 'itemTitle'}, 	
 	      {header: '类别',  align: 'center', width: 25, dataIndex: 'itemCategory',renderer:this.renderCategory},      
 	      {header: '内容描述',  align: 'left', dataIndex: 'itemContent',renderer:this.renderContent},
 	      {header: '重点、难点及需协调事宜', width: 40, hidden:true,align: 'left', dataIndex: 'itemOther'},
 	      {header: '备注',  align: 'left',width: 40, hidden:true,dataIndex: 'itemRemark'},
 	      {header: '计划实施时间',  align: 'center', width: 30,format: 'Y-m-d', hidden:true,dataIndex: 'planWork'},
 	      {header: '计划完成时间',  align: 'center', width: 30, format: 'Y-m-d',dataIndex: 'planFinish'},
 	      {header: '任务状态',  align: 'center', width: 25, format: 'Y-m-d',dataIndex: 'taskStatus',renderer:this.renderStatus}
 	];
	
	this.grid = new Ext.grid.GridPanel({
		store: this.storeItems,
        region: 'center',
        cm: new Ext.grid.ColumnModel(this.columns),
        sm: this.sm,
        viewConfig: { forceFit: true },
        scope:this
	});
	
	AuditPlan.WeeklyReport.superclass.constructor.call(this, {
		layout: 'border',
		title: '周计划部门上报',
        width: 700,
        height: 420,
        resizable: false,
        modal: true,
        autoScroll: true,
        closeAction: 'hide',
        items: [this.form, this.grid],
		buttons:[{
            text: '上报',
            cls: 'x-btn-text-icon btn-feedback',
            handler: this.onReport,
            scope: this
        }, {
            text: '关闭',
            cls: 'x-btn-text-icon btn-cancel',
            handler: this.hide.createDelegate(this, [])
        }]
    });
};
Ext.extend(AuditPlan.WeeklyReport, Ext.Window,{
	//取得计划信息	
	initWin:function(data,ids){
		//this.form.getForm().reset();
		this.form.getForm().setValues(data);
		this.planName.setValue(data.deptName+'周计划');
		this.planIds=ids;	
		this.grid.store.load({
			params:{planIds:ids}
		});		
	},
	//上报
	onReport: function(btn, event) {
		var records = this.grid.getSelectionModel().getSelections();
		if(records.length==0){
			Utils.warn2('请选择一条计划！',180);
			return ;
		}
		var ids='';
		var plans='';
		for(var i=0;i<records.length;i++){
			ids+=records[i].data['itemId']+',';
			plans+=records[i].data['planId']+',';
		}		
		var conn = new Ext.data.Connection({
  			url:'../auditplan/reportPlan.form?timesnap=' + Date.parse(new Date()),
  			method:'post'
  		})
  		var param=this.form.getForm().getFieldValues();
  		Utils.formatDate(param,'startDate');
  		Utils.formatDate(param,'endDate');
  		param['planIds']=plans;
  		param['itemIds']=ids;
  		//alert(Ext.util.JSON.encode(param));
		Utils.request(conn,{
  			params:param,
  			scope:this,
  			success:function(data){
  				Utils.info("操作成功！");
				this.scope.hide();
				this.scope.winParent.store.reload();
  			}
  		});  		
	}
});